#include<cstdio>
const int maxn=30010;
int p[maxn],r[maxn],h[maxn];
int find(int x)
{
	int t;
	if(p[x]!=x)
	{
		t=p[x];
		p[x]=find(p[x]);
		r[x]=r[x]+r[t];
	}
	return p[x];
}
void merge(int x,int y)
{
	int p1=find(x);
	int p2=find(y);
	p[p1]=p2;
	r[p1]=h[p2];	
	h[p2]+=h[p1];
}

int main()
{
	int n,m;
	scanf("%d",&m);
	for(int i=1;i<=maxn;i++){p[i]=i;r[i]=0;h[i]=1;}
	for(int i=1;i<=m;i++)
	{
			int x,y;
			char ch;
			scanf("%c%c",&ch,&ch);
			if(ch=='C')
			{
				scanf("%d",&x);
				int t=find(x);
				printf("%d\n",r[x]);
			}
			if(ch=='M')
			{
				scanf("%d%d",&x,&y);
				merge(x,y);
			}
	}
	return 0;
}
